/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tdc.visitor.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.tdc.department.entity.TdcDepartment;
import com.thinkgem.jeesite.modules.tdc.times.entity.TdcTimes;
import com.thinkgem.jeesite.modules.tdc.visitor.entity.TdcVisitorReservation;
import com.thinkgem.jeesite.modules.tdc.visitor.entity.TdcVisitorReservationJson;
import com.thinkgem.jeesite.modules.tdc.visitor.service.TdcVisitorReservationService;

import net.sf.json.JSONObject;

/**
 * 前台首页-预约Controller
 * @author 曹红
 * @version 2017-03-21
 */
@Controller
@RequestMapping(value = "/visitor")
public class TdcVisitorReservationController extends BaseController {

	@Autowired
	@Qualifier(value="tdcVisitorService")
	private TdcVisitorReservationService tdcVisitorReservationService;
	
	@ModelAttribute
	public TdcVisitorReservation get(@RequestParam(required=false) String id) {
		TdcVisitorReservation entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tdcVisitorReservationService.get(id);
		}
		if (entity == null){
			entity = new TdcVisitorReservation();
		}
		return entity;
	}
	
	/**
	 * 获得预约时间信息
	 */
	@RequestMapping(value = { "bespeakTimeTable" })
	public String bespeakTimeTable(Model model) {
		List<TdcTimes>times = tdcVisitorReservationService.getTimes(); 
		model.addAttribute("times", times);
		return "tdc/visitor/bespeakTimeTable";
	}
	
	/**
	 * 验证--预约开始、结束时间段
	 */
	@RequestMapping(value = { "checkTime" })
	public void checkTime(HttpServletRequest request, HttpServletResponse response){
		String bespeakStart = request.getParameter("bespeakStart");
		String bespeakEnd = request.getParameter("bespeakEnd");
		String bespeakType = request.getParameter("bespeakType");
		int status = tdcVisitorReservationService.checkTime(bespeakStart,bespeakEnd,bespeakType);
		try {
			response.getWriter().println(status);
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	
	
	/**
	 * 游客首页
	 * 
	 * @param TdcVisitorReservation
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"list", ""})
	public String list(TdcVisitorReservation TdcVisitorReservation, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TdcVisitorReservation> page = tdcVisitorReservationService.findPage(new Page<TdcVisitorReservation>(request, response), TdcVisitorReservation); 
		
		int todayCounts=tdcVisitorReservationService.todayCount();
		int arrivedCounts=tdcVisitorReservationService.arrivedCount();
		model.addAttribute("todayCounts", todayCounts);
		model.addAttribute("arrivedCounts", arrivedCounts);
		
		model.addAttribute("page", page);
		return "tdc/visitor/tdcHome";
	}
	/**
	 * ajax-查询指定预约编号的信息
	 */
	@RequestMapping(value = { "search" })
	public void search(HttpServletRequest request, HttpServletResponse response, Model model) {
		String bntmp = request.getParameter("bespeakNumber");
		TdcVisitorReservationJson tvr = tdcVisitorReservationService.getByBespeakNumber(bntmp);
		if(null == tvr){
			try {
				response.getWriter().print("noOne");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if(null == tvr.getBespeakNumber() || "".equals(tvr.getBespeakNumber())){
			try {
				response.getWriter().print("false");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			try {
				String json01 = JSONObject.fromObject(tvr).toString();
				response.getWriter().print(json01);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 律师预约
	 */
	@RequestMapping(value = { "law" })
	public String law(TdcVisitorReservation tdcVisitorReservation, Model model) {
		if(null == tdcVisitorReservation.getId() || "".equals(tdcVisitorReservation.getId())){
			tdcVisitorReservation.setId(IdGen.uuid());
		}
		model.addAttribute("saveStatus", "bespeaking");
		model.addAttribute("tdcVisitorReservation",tdcVisitorReservation);
		return "tdc/visitor/tdcLawBespeak";
	}
	/**
	 * 公检法预约
	 */
	@RequestMapping(value = { "check" })
	public String check(TdcVisitorReservation tdcVisitorReservation, Model model) {
		if(null == tdcVisitorReservation.getId() || "".equals(tdcVisitorReservation.getId())){
			tdcVisitorReservation.setId(IdGen.uuid());
		}
		List<TdcDepartment> depts = tdcVisitorReservationService.getDepts();
		model.addAttribute("saveStatus", "bespeaking");
		model.addAttribute("depts",depts);
		model.addAttribute("tdcVisitorReservation",tdcVisitorReservation);
		return "tdc/visitor/tdcCheckBespeak";
	}
	
	/**
	 * 保存预约的信息
	 * @param TdcVisitorReservation 预约信息
	 * @param model
	 * @return 返回保存的预约信息的对象
	 */
	@RequestMapping(value = "save")
	public String save(TdcVisitorReservation tdcVisitorReservation,HttpServletRequest request, HttpServletResponse response, Model model) {
		String dept = tdcVisitorReservation.getBespeakDept();
		/*保存二维码的路径*/
		String pathyyyymm = "/userfiles/1/images/images2code/" + TdcVisitorReservationService.yyyymm();
		String path = request.getSession().getServletContext().getRealPath(pathyyyymm);
		/*保存时还得set预约编码，未到，二维码*/
		tdcVisitorReservation = tdcVisitorReservationService.save(tdcVisitorReservation,path,pathyyyymm,request.getContextPath());
		/*保存状态：成功、失败*/
		if(null == tdcVisitorReservation){
			model.addAttribute("saveStatus", "faild");
		}else model.addAttribute("saveStatus", "success");
		model.addAttribute("tdcVisitorReservation", tdcVisitorReservation);
		if(null == dept || "".equals(dept))
			return "tdc/visitor/tdcLawBespeak";
		else return "tdc/visitor/tdcCheckBespeak";
	}
	
}